<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="utf-8">
    <title>商城后台</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/lib/layui-v2.5.5/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
    <link rel="stylesheet" th:href="@{/lib/layui_ext/dtree/dtree.css}" >
    <link rel="stylesheet" th:href="@{/lib/layui_ext/dtree/font/dtreefont.css}" >
    <link rel="stylesheet" th:href="@{/lib/zTree/css/zTreeStyle/zTreeStyle.css}" >


    <style>
        .layui-table-cell {
            height: auto !important;
            white-space: normal;
        }
        .ztree *{
            font: 14px Helvetica Neue,Helvetica,PingFang SC,Tahoma,Arial,sans-serif;
        }
    </style>

</head>
<body>

<span th:replace="common_page::common"></span>

<div class="layui-layout">
    <div class="layui-side ">

        <div class="layuimini-container" style="height:90%">
            <div class="layuimini-main">
                <div class="layui-card">
                    <div class="layui-card-header">分类树</div>
                    <div class="layui-card-body" >
                        <ul id="categoryTree" class="ztree"></ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="layui-body">

        <div class="layuimini-container">
            <div class="layuimini-main">
                <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
                    <legend>已选商品 <a id="selectProductCountTip">0</a>个</legend>
                </fieldset>
                <table class="layui-hide" id="selectShopProductTable" lay-filter="selectShopProductTableFilter"  ></table>

                <script type="text/html" id="selectShopProductTableBar">
                    <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a> &nbsp;&nbsp;
                </script>
            </div>
        </div>

        <div class="layuimini-container">
            <div class="layuimini-main">
                <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
                    <legend>商品搜索</legend>
                </fieldset>

                <fieldset class="table-search-fieldset">
                    <div style="margin: 10px 10px 10px 10px">
                        <form class="layui-form layui-form-pane" id="tableSearchForm" action="">
                            <input type="hidden" id="categoryId" name="categoryId" th:value="${categoryId}">
                            <input type="hidden" id="selectProductIds"  th:value="${selectProductIds}">
                            <div class="layui-form-item">

                                <div class="layui-inline">
                                    <label class="layui-form-label">店铺商品ID</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="id" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label" style="width:140px;">店铺商品UUID</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="uuid" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">商品名称</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="name" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">

                                <div class="layui-inline">
                                    <label class="layui-form-label">店铺ID</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="shopId" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button type="submit" id="queryButton" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                                    <button type="reset" class="layui-btn layui-btn-primary" id="data-reset-btn" ><i class="layui-icon"></i> 重 置</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </fieldset>


                <table class="layui-hide" id="shopProductTable" lay-filter="shopProductTableFilter"></table>

            </div>
        </div>
    </div>
</div>

<script th:src="@{/lib/layui-v2.5.5/layui.js}" charset="utf-8"></script>
<script th:src="@{/lib/jquery-3.4.1/jquery-3.4.1.min.js}" charset="utf-8"></script>
<script type="text/javascript" th:src="@{/lib/zTree/js/jquery.ztree.core.js}"></script>
<script type="text/javascript" th:src="@{/lib/zTree/js/jquery.ztree.excheck.js}"></script>
<script type="text/javascript" th:src="@{/lib/zTree/js/jquery.ztree.exedit.js}"></script>
<script>

    var toucan_component_select_product = new Array();

    /**
     * 拿到所选商品
     */
    function getComponentSelectProduct()
    {
        return toucan_component_select_product;
    }


    layui.config({
        base: basePath+'/lib/layui_ext/dtree/' //配置 layui 第三方扩展组件存放的基础目录
    }).extend({
        dtree: 'dtree' //定义该组件模块名
    }).use(['form', 'table','tree','dtree','jquery'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        var index;
        var tree = layui.tree;
        var loadding;
        var loaddingTree;
        var dtree = layui.dtree;
        var selectShopProductTable;
        var shopProductTable;

        $.fn.serializeJson=function(){
            var serializeObj={};
            var array=this.serializeArray();
            var str=this.serialize();
            $(array).each(function(){
                if(serializeObj[this.name]){
                    if($.isArray(serializeObj[this.name])){
                        serializeObj[this.name].push(this.value);
                    }else{
                        serializeObj[this.name]=[serializeObj[this.name],this.value];
                    }
                }else{
                    serializeObj[this.name]=this.value;
                }
            });
            return serializeObj;
        };


        function getFormParam()
        {
            var formParam = $("#tableSearchForm").serializeJson();
            var selectProductIdArray = null;
            if(toucan_component_select_product.length>0)
            {
                selectProductIdArray = new Array();
                for(var i=0;i<toucan_component_select_product.length;i++)
                {
                    var selectProduct = toucan_component_select_product[i];
                    selectProductIdArray.push(selectProduct.id);
                }
                formParam.selectProductIds =selectProductIdArray.join(",");
            }else{
                formParam.selectProductIds="";
            }
            return formParam;
        }

        initSelectProductTable();

        function initSelectProductTable(){
            var queryObj={};
            queryObj.ids=new Array();
            var selectProductIdsValue = $("#selectProductIds").val();
            if(selectProductIdsValue!=null&&selectProductIdsValue!="") {
                var selectProducts = selectProductIdsValue.split(",");
                for (var i = 0; i < selectProducts.length; i++) {
                    queryObj.ids.push(selectProducts[i]);
                }
                $.ajax({
                    url: "[[@{/}]]component/selectShopProduct/list/id",
                    type: "POST",
                    contentType: "application/json;charset=utf-8",
                    dataType: "json",
                    data: JSON.stringify(queryObj),
                    success: function (resp) {
                        toucan_component_select_product = resp.data;
                        initShopProductTable();
                    },
                    error: function () {

                    },
                    complete: function () {
                    }
                });
            }else{
                initShopProductTable();
            }

        }

        function initShopProductTable()
        {
            shopProductTable = table.render({
                elem: '#shopProductTable',
                url: "[[@{/}]]component/selectShopProduct/list",
                method:'POST',
                where:getFormParam(),
                contentType:"application/json",
                toolbar: '#toolbarDemo',
                defaultToolbar: ['filter',  {
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                done:function(res, curr, count)
                {
                    //隐藏全选按钮
                    $('th[data-field=0] div').replaceWith('<div class="layui-table-cell laytable-cell-5-0-0"><span>&nbsp;&nbsp;&nbsp;&ensp;</span></div>');

                },
                cols: [
                    [
                        {type: "checkbox", width: 50},
                        {field: 'id',  title: '店铺商品ID', width: 150},
                        {field: 'name',width: 400,  title: '商品名称',event:'openDetailDialogDevent',
                            templet: function (row) {
                                return "<a href='#'   style='color: blue;text-decoration:underline;'>"+row.name+"</a>";
                            }},
                        {field: 'httpMainPhotoFilePath',width:150,  title: '商品主图',
                            templet: function (row) {
                                if(row.httpMainPhotoFilePath!=null) {
                                    return "<a href='" + row.httpMainPhotoFilePath + "' target='_blank'><img src='" + row.httpMainPhotoFilePath + "' width='100' height='100'  /></a>";
                                }
                                return "";
                            }},
                        {field: 'uuid',width: 300,  title: '店铺商品UUID'},
                        {field: 'categoryPath',width: 350,  title: '分类路径'},
                        {field: 'categoryName',width: 150,  title: '分类名称'},
                        {field: 'brandHttpLogo',width:150,  title: '品牌LOGO',
                            templet: function (row) {
                                if(row.brandHttpLogo!=null) {
                                    return "<a href='" + row.brandHttpLogo + "' target='_blank'><img src='" + row.brandHttpLogo + "' width='100' height='100'  /></a>";
                                }
                                return "";
                            }},
                        {field: 'brandChineseName',width: 150,  title: '品牌中文名称'},
                        {field: 'brandEnglishName',width: 150,  title: '品牌英文名称'},
                        {field: 'shopCategoryPath',width: 150,  title: '店铺内分类路径'},
                        {field: 'shopCategoryName',width: 150,  title: '店铺内分类名称'},
                        {field: 'productUuid',width: 150,  title: '平台商品的UUID'},
                        {field: 'productId',width: 150,  title: '平台商品ID'},
                        {field: 'attributes',width: 550,  title: '属性列表'},
                        {field: 'status',width: 150,   title: '上架状态',
                            templet: function (row) {
                                if(row.status == null){
                                    return "";
                                }
                                if(row.status == "0"){
                                    return "<a style='color:red'>未上架</a>";
                                }
                                if(row.status == "1"){
                                    return "<a style='color:green'>已上架</a>";
                                }
                                return row.status;
                            }
                        },
                        {field: 'shopId',width: 300,  title: '店铺ID'},
                        {field: 'shopName',width: 300,  title: '店铺名称'},
                        {field: 'sellerNo',width: 150,   title: '卖家编码'},
                        {field: 'articleNumber',width: 150, title: '货号'},
                        {field: 'payMethod',width: 150,   title: '付款方式',
                            templet: function (row) {
                                if(row.payMethod == null){
                                    return "";
                                }
                                if(row.payMethod == "1"){
                                    return "一口价(普通交易模式)";
                                }
                                return row.payMethod;
                            }
                        },
                        {field: 'buckleInventoryMethod',width: 150,   title: '库存计数',
                            templet: function (row) {
                                if(row.buckleInventoryMethod == null){
                                    return "";
                                }
                                if(row.buckleInventoryMethod == "1"){
                                    return "买家拍下减库存";
                                }
                                if(row.buckleInventoryMethod == "2"){
                                    return "买家付款减库存";
                                }
                                return row.buckleInventoryMethod;
                            }
                        },
                        {field: 'giveInvoice',width: 150,   title: '开发票',
                            templet: function (row) {
                                if(row.giveInvoice == null){
                                    return "";
                                }
                                if(row.giveInvoice == "1"){
                                    return "支持";
                                }
                                if(row.giveInvoice == "0"){
                                    return "不支持";
                                }
                                return row.giveInvoice;
                            }
                        },
                        {field: 'changeOrReturn',width: 150,   title: '退换货承诺',
                            templet: function (row) {
                                if(row.changeOrReturn == null){
                                    return "";
                                }
                                if(row.changeOrReturn == "1"){
                                    return "承诺";
                                }
                                if(row.changeOrReturn == "0"){
                                    return "不承诺";
                                }
                                return row.changeOrReturn;
                            }
                        },
                        {field: 'etractMethod',width: 150,   title: '提取方式',
                            templet: function (row) {
                                var etractMethodText = "";
                                if(row.etractMethod!=null) {
                                    if (row.etractMethod.indexOf("1") != -1) {
                                        etractMethodText += "用物流配送"
                                    }
                                }
                                return etractMethodText;
                            }
                        },
                        {field: 'createUserId',width: 300, title: '店铺掌柜ID'},
                        {field: 'createDate', width: 150, title: '创建时间'},
                        {field: 'updateDate', width: 150, title: '修改时间'}
                    ]
                ],
                limits: [15,30,100,200],
                limit: 15,
                page: true,
                skin: 'line'
            });

            initSelectShopProductTable();
        }


        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {

            if (shopProductTable!= null) {
                shopProductTable.where = {};  //置空where
            }
            //执行搜索重载
            shopProductTable.reload({
                page: {
                    curr: 1
                }
                , where: getFormParam(),
                done:function(res, curr, count)
                {
                    //隐藏全选按钮
                    $('th[data-field=0] div').replaceWith('<div class="layui-table-cell laytable-cell-5-0-0"><span>&nbsp;&nbsp;&nbsp;&ensp;</span></div>');
                }
            });

            return false;
        });



        $("#data-reset-btn").click(function(){
            $("#categoryId").val("");
            $("#tableSearchForm")[0].reset();
            layui.form.render();
        });


        var setting = {
            async: {
                enable: true,
                url:"[[@{/}]]component/selectShopProduct/query/category/tree/pid",
                autoParam:["id", "pid", "name"],
                dataFilter: filter
            },
            data:{
                simpleData:{
                    enable: true,
                    idKey:'id',
                    pIdKey:'parentId',
                    rootPId: -1
                }
            },
            view:{
                showIcon: true
            },
            callback: {
                onClick: categoryTreeClick
            }
        };

        function categoryTreeClick(event, treeId, treeNode) {
            // $("#inp").val("id="+treeNode.id + ", name=" + treeNode.name + ",pId=" + treeNode.pId);
            $("#categoryId").val(treeNode.id);
            $("#queryButton").click();
        }

        initCategoryTree();

        function filter(treeId, parentNode, data) {
            return data.data;
        }
        //初始化树
        function initCategoryTree(){

            loaddingTree = layer.load();
            $.ajax({
                url:"[[@{/}]]component/selectShopProduct/query/category/tree/pid",
                type:"POST",
                dataType: "json",
                success: function(data){
                    var zTreeObj = $.fn.zTree.init($("#categoryTree"),setting, data.data);
                    layer.close(loaddingTree);
                },
                error: function(){

                },
                complete:function()
                {
                    layer.close(loaddingTree);
                }
            });
        }



        //监听表格复选框选择
        table.on('checkbox(shopProductTableFilter)', function (obj) {
            //拿到当前所有选中行
            var selectRow = obj.data;
            var isFind=false;
            if(!obj.checked) {
                for (var i = 0; i < toucan_component_select_product.length; i++) {
                    var selectProduct = toucan_component_select_product[i];
                    if (selectProduct.id == selectRow.id) {
                        toucan_component_select_product.splice(i, 1);
                        isFind = true;
                        break;
                    }
                }
            }
            if(!isFind)
            {
                toucan_component_select_product.push(selectRow);
            }

            //重新绘制已选商品
            initSelectShopProductTable();
        });

        table.on('tool(shopProductTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event == 'detail') {
                openDetailDialog(data.id);
            }else if(obj.event=="openDetailDialogDevent")
            {
                openDetailDialog(data.id);
            }
        });



        function openDetailDialog(id)
        {
            var index = layer.open({
                title: '商品详情',
                type: 2,
                shade: 0.2,
                maxmin:true,

                area: ['100%', '100%'],
                content: '[[@{/}]]component/selectShopProduct/detailPage/'+id
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;

        }



        function initSelectShopProductTable() {
            selectShopProductTable = table.render({
                elem: '#selectShopProductTable',
                defaultToolbar: [],
                cols: [
                    [
                        {field: 'id',  title: '店铺商品ID', width: 150},
                        {field: 'name',width: 400,  title: '商品名称',event:'openDetailDialogDevent',
                            templet: function (row) {
                                return "<a href='#'   style='color: blue;text-decoration:underline;'>"+row.name+"</a>";
                            }},
                        {field: 'httpMainPhotoFilePath',width:150,  title: '商品主图',
                            templet: function (row) {
                                if(row.httpMainPhotoFilePath!=null) {
                                    return "<a href='" + row.httpMainPhotoFilePath + "' target='_blank'><img src='" + row.httpMainPhotoFilePath + "' width='100' height='100'  /></a>";
                                }
                                return "";
                            }},
                        {field: 'uuid',width: 300,  title: '店铺商品UUID'},
                        {field: 'categoryPath',width: 350,  title: '分类路径'},
                        {field: 'categoryName',width: 150,  title: '分类名称'},
                        {title: '操作', minWidth: 300, toolbar: '#selectShopProductTableBar', align: "center"}
                    ]
                ],
                data: toucan_component_select_product,
                skin: 'line'
            });

            $("#selectProductCountTip").text(toucan_component_select_product.length);
        }



        table.on('tool(selectShopProductTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event == 'delete') {
                layer.confirm("确定删除"+data.name+"?", function (index) {
                    if(toucan_component_select_product!=null&&toucan_component_select_product.length>0)
                    {
                        for(var i=0;i<toucan_component_select_product.length;i++)
                        {
                            var row = toucan_component_select_product[i];
                            if(data.id==row.id)
                            {
                                toucan_component_select_product.splice(i, 1);
                                break;
                            }
                        }
                    }
                    initSelectShopProductTable();
                    layer.close(index);
                    $("#queryButton").click();
                });
            }else if(obj.event=="openDetailDialogDevent")
            {
                openDetailDialog(data.id);
            }
        });

    });


</script>


</body>
</html>